package com.sheng.batch;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import javax.naming.MalformedLinkException;
import java.util.ArrayList;
import java.util.List;


public class Producer {
    public static void main(String[] args) throws Exception {
        //1.创建一个发送消息的对象Producer
        DefaultMQProducer producer = new DefaultMQProducer("group");
        //2.设定发送的命名服务器地址
        producer.setNamesrvAddr("localhost:9876");
        //3.启动发送的服务
        producer.start();
        List<Message> list = new ArrayList<>();
        //4.创建要发送的消息对象,指定topic,指定内容body
        Message msg1 = new Message("topic", ("批量消息  1:  hello rocketmq").getBytes("UTF-8"));
        Message msg2 = new Message("topic", ("批量消息  3:  hello rocketmq").getBytes("UTF-8"));
        Message msg3 = new Message("topic", ("批量消息  2:  hello rocketmq").getBytes("UTF-8"));
        //发送消息

        list.add(msg1);
        list.add(msg2);
        list.add(msg3);
        SendResult result = producer.send(list);
        System.out.println("返回结果: " + result);


        //关闭连接
        producer.shutdown();

    }
}
